/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import supermarketv0_class.*;

/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class ProductManagement extends javax.swing.JFrame {
 SuperMarketMainView1 mainscreen;
 Product product;
 List products;
 String b1,b2,b3;
    /**
     * Creates new form ProductManagement
     */
    public ProductManagement(SuperMarketMainView1  mainscreen,Product product) {
         this.mainscreen=mainscreen;
         this.product=product;
        initComponents();
        
          //εμφάνηση τιμών σε πίνακα 
        products= mainscreen.db.readProduct();
        if(products.isEmpty()){//απενεργοποίηση πίνακα και κουμπιών
            jTable1.disable();
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);
        }
        Object[][] data=new Object[products.size()][5];//εδώ θα μπουν οι γραμμές του πίνακα
         int i=0;
        Iterator itr=products.iterator();
        while (itr.hasNext()){
            product=(Product)itr.next();
            //ΟΙ ΣΤΗΛΕΣ ΤΟΥ ΠΙΝΑΚΑ
            data[i][0]=product.getProductId();
            data[i][1]=product.getName();
            data[i][2]= product.getCode();
            data[i][3]= product.getPoints();
            data[i][4]= product.getPrice();
            
           
            i++;
            String[] columnNames = {"ID","Όνομα","Κωδικός","Πόντοι","Τιμή"};//τίτλοι των στηλών του πίνακα
            jTable1.setModel(new javax.swing.table.DefaultTableModel( data,columnNames));//δημιουργία πίνακα
            jTable1.repaint();//επανασχεδιασμός πίνακα
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(ProductManagement.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(jButton4)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(89, 89, 89))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addGap(55, 55, 55)
                        .addComponent(jButton4)
                        .addGap(79, 79, 79)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:Δημιουργία Προϊόντος
         b1= jButton1.getText();
          this.dispose();
          mainscreen.addproduct= new AddProduct(mainscreen,null,b1);
          mainscreen.addproduct.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          mainscreen.addproduct.setVisible(true);
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:Τροποποίηση Προϊόντων 
         b2=jButton2.getText();
        int selectedproduct=jTable1.getSelectedRow();//αριθμός επιλεγμένης γραμμής
        if(selectedproduct>=0){
            product= (Product)products.get(selectedproduct);
            mainscreen.addproduct=new AddProduct(mainscreen,product,b2);
            mainscreen.addproduct.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            mainscreen.addproduct.setVisible(true);
           dispose();
        }
        else{
        //ΑΝ ΔΕΝ ΕΧΕΙΣ ΕΠΙΛΕΞΕΙ ΓΡΑΜΜΗ
            JOptionPane.showMessageDialog(null, "Επιλέξτε Προϊόν", "Error", JOptionPane.WARNING_MESSAGE);
}
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:Διαγραφή Προϊόντος
         b3= jButton3.getText();
         int selectedproduct=jTable1.getSelectedRow();//αριθμός επιλεγμένης γραμμής
        if(selectedproduct>=0){
            product= (Product)products.get(selectedproduct);
            mainscreen.addproduct=new AddProduct(mainscreen,product,b3);
            mainscreen.addproduct.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            mainscreen.addproduct.setVisible(true);
           dispose();
        }
        else{
        //ΑΝ ΔΕΝ ΕΧΕΙΣ ΕΠΙΛΕΞΕΙ ΓΡΑΜΜΗ
            JOptionPane.showMessageDialog(null, "Επιλέξτε Προϊόν", "Error", JOptionPane.WARNING_MESSAGE);
}
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:Κλείσιμο
        dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
